package com.rdc.qingkou.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.rdc.qingkou.Application.MyApplication;
import com.rdc.qingkou.R;
import com.rdc.qingkou.adapter.MyFragmentStatePagerAdapter;
import com.rdc.qingkou.utils.TemplateSharePreferenceUtils;
import com.rdc.qingkou.view.fragment.TemplateFragment;
import com.rdc.qingkou.widget.GalleryPageTransformer;

import java.util.ArrayList;
import java.util.List;

public class TemplateActivity extends AppCompatActivity {



    private ViewPager mViewPager;
    private MyFragmentStatePagerAdapter mAdapter;
    private ImageView mDot1;
    private ImageView mDot2;
    private Button mBack;
    private Button mConfirm;
    private int mPosition;
    private static final String TAG = "TemplateActivity";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.addActivity(this);
        setContentView(R.layout.send_template);
        initViews();
    }

    private void initViews() {
        mViewPager = (ViewPager) findViewById(R.id.share_viewpager);
        mDot1 = (ImageView)findViewById(R.id.iv_dot1);
        mDot2 = (ImageView)findViewById(R.id.iv_dot2);
        mBack = (Button)findViewById(R.id.btn_back);
        mConfirm = (Button)findViewById(R.id.btn_confirm);
        List<Fragment> list = new ArrayList<>();
        TemplateFragment fragment1 = new TemplateFragment();
        fragment1.setData(R.layout.send_template_one);
        list.add(fragment1);
        TemplateFragment fragment2 = new TemplateFragment();
        fragment2.setData(R.layout.send_template_two);
        list.add(fragment2);
        mAdapter = new MyFragmentStatePagerAdapter(getSupportFragmentManager(), list);
        mViewPager.setAdapter(mAdapter);
        mViewPager.setOffscreenPageLimit(3);
        mViewPager.setPageTransformer(false, new GalleryPageTransformer());
        mViewPager.setPageMargin(-80);

        final TemplateSharePreferenceUtils utils = new TemplateSharePreferenceUtils(this);

        mDot1.setEnabled(true);
        mDot2.setEnabled(false);
        mPosition = 0;
        Log.e(TAG, "initViews: "+utils.getTemplateInt() );
        if (utils.getTemplateInt() == 1) {

            mViewPager.setCurrentItem(1);
            mPosition = 1;
        }

        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                   if (position==0){
                       mDot1.setEnabled(true);
                       mDot2.setEnabled(false);
                       mPosition = 0;
                       Log.e(TAG, "onPageSelected: "+mPosition );

                   }
                   if (position==1){
                       mDot1.setEnabled(false);
                       mDot2.setEnabled(true);
                       mPosition = 1;
                       Log.e(TAG, "onPageSelected: "+mPosition );

                   }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                utils.putTemlateInt(mPosition);
                Log.e(TAG, "onClick: "+ utils.getTemplateInt());
                finish();
            }
        });
    }

}
